<?php
set_time_limit(0);
include_once (PATH_SQL . 'print_category_sql.php');
include_once (PATH_MODEL . 'print_category_model.php');
include_once (PATH_VIEW_ADMIN . 'print_category_view.php');
$oModel = new ModelPrintCategory();
$oView = new ViewPrintCategory($oModel);
$oModel->setData('page_size',50);
$oModel->setData('pagegroup_size',5);
$oModel->setData('stylesheet',"paging");
$alphabet=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$sAction = $_REQUEST['sAction'];
//$oView->displatChooseCategoryIndex($oModel);
switch($sAction) {

    case 'do_generate_pro_id': {
            $cat_id=$_POST['yahooID'];
            $owner_id=isset($_POST['shop_owner_id'])? $_POST['shop_owner_id']:0;
            $today=date('jn');
            $last4=substr($cat_id,-4);
            //$num = $oModel->getLastestNumberOfProduct();
            $num = $oModel->getLastestNumberOfProduct($owner_id);
            if($num=="Z99") {
                $num=0;
            }
            else if($num=="") {
                    $num=0;
                }
                else {
                    $first=substr($num,0,1);
                    $odd=ord($first)-ord("A");
                    $last=substr($num,1,strlen($num)-1);
                    $num = $odd*100 + $last;
                }
            $num++;
            $modNum=$num%100;
            if(strlen($modNum)==1) {
                $modNum="0".$modNum;
            }
            $divNum=floor($num/100);

            $newID=$alphabet[$divNum].$modNum;
            while($oModel->checkExistID($newID,$owner_id)) {
                $num++;
                $modNum=$num%100;
                if(strlen($modNum)==1) {
                    $modNum="0".$modNum;
                }
                $divNum=floor($num/100);
                $newID=$alphabet[$divNum].$modNum;
            }
            $date_c=date("d").date("m"). date("Y");
            $pre_fix="";
            for($i=strlen($num);$i<4;$i++) {
                $pre_fix.="0";
            }
            $barCode=$date_c.$pre_fix.$num;
            //$gen_pro_id=$last4.''.$today.''.$num;

            $check = $oModel->doInsertGenCategoryId($newID,$cat_id,$owner_id);
            if(!$check) {
                echo "";
            }else {
                $row=$oModel->getInfoCategoryByYahooID($cat_id);
                echo '<form name="frmPrint" action="">
					<input name="PID" type="hidden" value="'.$newID.'">
					<input name="YID" type="hidden" value="'.$row['product_category_yahoo_id'].'">
					<input name="CName" type="hidden" value="'.$row['product_category_name'].'">
					<input name="BarCode" type="hidden" value="'.$barCode.'">
					<input name="DPrint" type="hidden" value="'.date("m/d/Y").'">
					</form>';
            }
            exit();
            break;
        }
    case "loadChildCategory":
        $oView->loadChildCategory($oModel);
        break;
    case "loadAlphabetCategory":
        $oView->loadAlphabetCategory($oModel);
        break;
    case "loadCurrentTab":
        $oView->loadCurrentTab($oModel);
        break;
    case "loadPageNav":
        $oView->loadPageNav($oModel);
        break;
    default:
        $oView->displaySelectPage($oModel);
        break;
}
$oView->displayPage($oModel);
